<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单词" prop="word">
        <el-input
          v-model="queryParams.word"
          placeholder="请输入单词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="词组" prop="collocation">
        <el-input
          v-model="queryParams.collocation"
          placeholder="请输入词组"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单词翻译" prop="wordMeaning">
        <el-input
          v-model="queryParams.wordMeaning"
          placeholder="请输入单词翻译"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="词组翻译" prop="collocationMeaning">
        <el-input
          v-model="queryParams.collocationMeaning"
          placeholder="请输入词组翻译"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="词性" prop="partOfSpeech">
        <el-input
          v-model="queryParams.partOfSpeech"
          placeholder="请输入词性"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="语言分类" prop="site">
        <el-input
          v-model="queryParams.site"
          placeholder="请输入语言分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="音标" prop="pronunciation">
        <el-input
          v-model="queryParams.pronunciation"
          placeholder="请输入音标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单词语音" prop="wordVoice">
        <el-input
          v-model="queryParams.wordVoice"
          placeholder="请输入单词语音"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="词组语音" prop="collocationVoice">
        <el-input
          v-model="queryParams.collocationVoice"
          placeholder="请输入词组语音"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="例句语音" prop="sentenceVoice">
        <el-input
          v-model="queryParams.sentenceVoice"
          placeholder="请输入例句语音"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类ID" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入分类ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级分类ID" prop="subCategoryId">
        <el-input
          v-model="queryParams.subCategoryId"
          placeholder="请输入二级分类ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['language:vocabulary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['language:vocabulary:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['language:vocabulary:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['language:vocabulary:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vocabularyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="单词" align="center" prop="word" />
      <el-table-column label="词组" align="center" prop="collocation" />
      <el-table-column label="例句" align="center" prop="sentence" />
      <el-table-column label="例句翻译" align="center" prop="sentenceMeaning" />
      <el-table-column label="单词翻译" align="center" prop="wordMeaning" />
      <el-table-column label="词组翻译" align="center" prop="collocationMeaning" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="词性" align="center" prop="partOfSpeech" />
      <el-table-column label="语言分类" align="center" prop="site" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="音标" align="center" prop="pronunciation" />
      <el-table-column label="单词语音" align="center" prop="wordVoice" />
      <el-table-column label="词组语音" align="center" prop="collocationVoice" />
      <el-table-column label="例句语音" align="center" prop="sentenceVoice" />
      <el-table-column label="分类ID" align="center" prop="categoryId" />
      <el-table-column label="二级分类ID" align="center" prop="subCategoryId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['language:vocabulary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['language:vocabulary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改词条对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单词" prop="word">
          <el-input v-model="form.word" placeholder="请输入单词" />
        </el-form-item>
        <el-form-item label="词组" prop="collocation">
          <el-input v-model="form.collocation" placeholder="请输入词组" />
        </el-form-item>
        <el-form-item label="例句" prop="sentence">
          <el-input v-model="form.sentence" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="例句翻译" prop="sentenceMeaning">
          <el-input v-model="form.sentenceMeaning" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="单词翻译" prop="wordMeaning">
          <el-input v-model="form.wordMeaning" placeholder="请输入单词翻译" />
        </el-form-item>
        <el-form-item label="词组翻译" prop="collocationMeaning">
          <el-input v-model="form.collocationMeaning" placeholder="请输入词组翻译" />
        </el-form-item>
        <el-form-item label="词性" prop="partOfSpeech">
          <el-input v-model="form.partOfSpeech" placeholder="请输入词性" />
        </el-form-item>
        <el-form-item label="语言分类" prop="site">
          <el-input v-model="form.site" placeholder="请输入语言分类" />
        </el-form-item>
        <el-form-item label="音标" prop="pronunciation">
          <el-input v-model="form.pronunciation" placeholder="请输入音标" />
        </el-form-item>
        <el-form-item label="单词语音" prop="wordVoice">
          <el-input v-model="form.wordVoice" placeholder="请输入单词语音" />
        </el-form-item>
        <el-form-item label="词组语音" prop="collocationVoice">
          <el-input v-model="form.collocationVoice" placeholder="请输入词组语音" />
        </el-form-item>
        <el-form-item label="例句语音" prop="sentenceVoice">
          <el-input v-model="form.sentenceVoice" placeholder="请输入例句语音" />
        </el-form-item>
        <el-form-item label="分类ID" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入分类ID" />
        </el-form-item>
        <el-form-item label="二级分类ID" prop="subCategoryId">
          <el-input v-model="form.subCategoryId" placeholder="请输入二级分类ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVocabulary, getVocabulary, delVocabulary, addVocabulary, updateVocabulary } from "@/api/language/vocabulary";

export default {
  name: "Vocabulary",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 词条表格数据
      vocabularyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        word: null,
        collocation: null,
        sentence: null,
        sentenceMeaning: null,
        wordMeaning: null,
        collocationMeaning: null,
        status: null,
        partOfSpeech: null,
        site: null,
        type: null,
        pronunciation: null,
        wordVoice: null,
        collocationVoice: null,
        sentenceVoice: null,
        categoryId: null,
        subCategoryId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询词条列表 */
    getList() {
      this.loading = true;
      listVocabulary(this.queryParams).then(response => {
        this.vocabularyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        updateTime: null,
        word: null,
        collocation: null,
        sentence: null,
        sentenceMeaning: null,
        wordMeaning: null,
        collocationMeaning: null,
        category: null,
        subCategory: null,
        status: null,
        wordVoiceOrg: null,
        collocationVoiceOrg: null,
        sentenceVoiceOrg: null,
        partOfSpeech: null,
        site: null,
        type: null,
        pronunciation: null,
        wordVoice: null,
        collocationVoice: null,
        sentenceVoice: null,
        categoryId: null,
        subCategoryId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加词条";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVocabulary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改词条";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVocabulary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVocabulary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除词条编号为"' + ids + '"的数据项？').then(function() {
        return delVocabulary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('language/vocabulary/export', {
        ...this.queryParams
      }, `vocabulary_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
